package com.huyaanalysisplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huyaanalysisplatform.entity.TContribution;
import com.huyaanalysisplatform.entity.TTop10Anchor;
import com.huyaanalysisplatform.mapper.TContributionMapper;
import com.huyaanalysisplatform.service.TContributionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huyaanalysisplatform.service.TTop10AnchorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Huadi Group 12
 * @since 2021-08-01
 */
@Service
public class TContributionServiceImpl extends ServiceImpl<TContributionMapper, TContribution> implements TContributionService {
    @Autowired
    TTop10AnchorService tTop10AnchorService;

    @Autowired
    TContributionService tContributionService;

    public Map queryFirst() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "网游竞技");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Long roomNumber = tTop10AnchorListForWeb.get(i).getRoomNumber();
            QueryWrapper<TContribution> queryWrapper1 = new QueryWrapper<TContribution>()
                    .eq("roomNumber", roomNumber);
            List<TContribution> tContributions = tContributionService.list(queryWrapper1);
            int total = 0;
            for (int j = 0; j < tContributions.size(); j++) {
                total += tContributions.get(j).getScore();
            }
            if (tContributions.size() != 0)
                total = total / tContributions.size();
            Map map1 = new HashMap();
            map1.put("totalHot", total);
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map querySecond() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "单机热游");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Long roomNumber = tTop10AnchorListForWeb.get(i).getRoomNumber();
            QueryWrapper<TContribution> queryWrapper1 = new QueryWrapper<TContribution>()
                    .eq("roomNumber", roomNumber);
            List<TContribution> tContributions = tContributionService.list(queryWrapper1);
            int total = 0;
            for (int j = 0; j < tContributions.size(); j++) {
                total += tContributions.get(j).getScore();
            }
            if (tContributions.size() != 0)
                total = total / tContributions.size();
            Map map1 = new HashMap();
            map1.put("totalHot", total);
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryThird() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "娱乐天地");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Long roomNumber = tTop10AnchorListForWeb.get(i).getRoomNumber();
            QueryWrapper<TContribution> queryWrapper1 = new QueryWrapper<TContribution>()
                    .eq("roomNumber", roomNumber);
            List<TContribution> tContributions = tContributionService.list(queryWrapper1);
            int total = 0;
            for (int j = 0; j < tContributions.size(); j++) {
                total += tContributions.get(j).getScore();
            }
            if (tContributions.size() != 0)
                total = total / tContributions.size();
            Map map1 = new HashMap();
            map1.put("totalHot", total);
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryFourth() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "手游休闲");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Long roomNumber = tTop10AnchorListForWeb.get(i).getRoomNumber();
            QueryWrapper<TContribution> queryWrapper1 = new QueryWrapper<TContribution>()
                    .eq("roomNumber", roomNumber);
            List<TContribution> tContributions = tContributionService.list(queryWrapper1);
            int total = 0;
            for (int j = 0; j < tContributions.size(); j++) {
                total += tContributions.get(j).getScore();
            }
            if (tContributions.size() != 0)
                total = total / tContributions.size();
            Map map1 = new HashMap();
            map1.put("totalHot", total);
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryFirstHot() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "网游竞技")
                .orderByDesc("totalHot");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Map map1 = new HashMap();
            map1.put("title", tTop10AnchorListForWeb.get(i).getTitle());
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            map1.put("totalHot", tTop10AnchorListForWeb.get(i).getTotalHot());
            map1.put("roomNumber", tTop10AnchorListForWeb.get(i).getRoomNumber());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map querySecondHot() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "单机热游")
                .orderByDesc("totalHot");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Map map1 = new HashMap();
            map1.put("title", tTop10AnchorListForWeb.get(i).getTitle());
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            map1.put("totalHot", tTop10AnchorListForWeb.get(i).getTotalHot());
            map1.put("roomNumber", tTop10AnchorListForWeb.get(i).getRoomNumber());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryThirdHot() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "娱乐天地")
                .orderByDesc("totalHot");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Map map1 = new HashMap();
            map1.put("title", tTop10AnchorListForWeb.get(i).getTitle());
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            map1.put("totalHot", tTop10AnchorListForWeb.get(i).getTotalHot());
            map1.put("roomNumber", tTop10AnchorListForWeb.get(i).getRoomNumber());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryFourthHot() {
        List<Map> tableData = new ArrayList<>();
        QueryWrapper<TTop10Anchor> queryWrapper = new QueryWrapper<TTop10Anchor>()
                .eq("areaName", "手游休闲")
                .orderByDesc("totalHot");
        List<TTop10Anchor> tTop10AnchorListForWeb = tTop10AnchorService.list(queryWrapper);
        for (int i = 0; i < tTop10AnchorListForWeb.size(); i++) {
            Map map1 = new HashMap();
            map1.put("title", tTop10AnchorListForWeb.get(i).getTitle());
            map1.put("nick", tTop10AnchorListForWeb.get(i).getNick());
            map1.put("totalHot", tTop10AnchorListForWeb.get(i).getTotalHot());
            map1.put("roomNumber", tTop10AnchorListForWeb.get(i).getRoomNumber());
            tableData.add(map1);
        }
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map queryContributions(Map<String, Long> map) {
        List<Map> contribution = new ArrayList<>();
        Long roomNumber = map.get("roomNumber");
        QueryWrapper<TContribution> queryWrapper = new QueryWrapper<TContribution>()
                .eq("roomNumber", roomNumber)
                .orderByDesc("score");
        List<TContribution> tContributions = tContributionService.list(queryWrapper);
        for (int i = 0; i < tContributions.size(); i++) {
            Map map1 = new HashMap();
            map1.put("score", tContributions.get(i).getScore());
            map1.put("nick", tContributions.get(i).getNick());
            map1.put("number", i + 1);
            contribution.add(map1);
        }
        Map map1 = new HashMap();
        map1.put("tableData", contribution);
        return map1;
    }

}
